package com.le.dub.controller;

import cn.hutool.core.util.ObjectUtil;
import com.le.dub.common.DateUtils;
import com.le.dub.common.Response;
import com.le.dub.conf.UserUtil;
import com.le.dub.mobile.UserDubCollect;
import com.le.dub.mobile.bo.DubRoleListBO;
import com.le.dub.mobile.vo.DubRoleVO;
import com.le.dub.service.IDubRoleService;
import com.le.dub.service.IUserDubCollectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 用户声音收藏
 */
@RestController
@RequestMapping("/app/userDubCollect")
public class UserDubCollectController {
    @Resource
    private IUserDubCollectService userDubCollectService;

    @Resource
    private IDubRoleService dubRoleService;
    /**
     * 收藏/取消收藏
     *
     * @param request
     * @param roleId
     * @return
     */
    @PostMapping("/collect")
    public Response collect(HttpServletRequest request, @RequestParam("roleId") Long roleId) {


        Long userId = UserUtil.getUserId(request);
        UserDubCollect userDubCollect=userDubCollectService.lambdaQuery().eq(UserDubCollect::getUserId,userId).eq(UserDubCollect::getRoleId,roleId).one();
        if(ObjectUtil.isNull(userDubCollect)){
            userDubCollect=new UserDubCollect();
            userDubCollect.setUserId(userId);
            userDubCollect.setRoleId(roleId);
            userDubCollect.setCreateTime(new Date());
            this.userDubCollectService.save(userDubCollect);
        }else {
            this.userDubCollectService.lambdaUpdate().eq(UserDubCollect::getUserId,userId).eq(UserDubCollect::getRoleId,roleId).remove();
        }

        return Response.success();
    }



    /**
     * 获取我的收藏声音列表
     *
     * @return
     */


    @RequestMapping(value = "/dubCollectList")
    public Response dubCollectList(HttpServletRequest request,@RequestBody  DubRoleListBO dubRoleListBO) {
        Long userId = UserUtil.getUserId(request);
        dubRoleListBO.setUserId(userId);
        List<DubRoleVO> dubCollectList=dubRoleService.dubCollectList(dubRoleListBO);
        for (DubRoleVO vo : dubCollectList) {
            vo.setCreateTimeFormat(DateUtils.format(vo.getCreateTime()));
        }
        return Response.success(dubCollectList);
    }
}
